package com.jinsp.webviewservice.webview;

import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by luoshuai on 12/30/21
 * <p>
 * 配置WebView信息
 */
public class WebViewConfig implements Parcelable {

    public static final String TAG = "WebViewConfig";

    private boolean isShowTitleBar;     //是否显示标题栏
    private String titleName;           //标题栏名称
    private int backDrawableId;         //左侧返回图标
    private String url;                 //链接地址
    private boolean isCanRefresh;       //是否可以刷新页面
    private int titleTextColor, titleBackground;    //标题栏文字颜色以及标题栏背景色
    private int refreshColor;           //刷新进度条颜色
    private String networkDisconnectedTip;  //网络未连接提示
    private int networkDisconnectedDrawable;    //网络未连接图标
    private String jsWebViewName;       //JS中webView名称

    public boolean isShowTitleBar() {
        return isShowTitleBar;
    }

    public String getTitleName() {
        return titleName;
    }

    public int getBackDrawableId() {
        return backDrawableId;
    }

    public String getUrl() {
        return url;
    }

    public boolean isCanRefresh() {
        return isCanRefresh;
    }

    public int getTitleTextColor() {
        return titleTextColor;
    }

    public int getTitleBackground() {
        return titleBackground;
    }

    public int getRefreshColor() {
        return refreshColor;
    }

    public String getNetworkDisconnectedTip() {
        return networkDisconnectedTip;
    }

    public int getNetworkDisconnectedDrawable() {
        return networkDisconnectedDrawable;
    }

    public String getJsWebViewName() {
        return jsWebViewName;
    }

    public static class Builder {

        private boolean isShowTitleBar = true;
        private String titleName = "WebView";
        private int backDrawableId;
        private String url;
        private boolean isCanRefresh = true;
        private int titleTextColor = Color.BLACK;
        private int titleBackground = Color.WHITE;
        private int refreshColor = Color.BLACK;
        private String networkDisconnectedTip = "NetworkDisconnected";
        private int networkDisconnectedDrawable;
        private String jsWebViewName = "webView";

        public Builder setShowTitleBar(boolean showTitleBar) {
            isShowTitleBar = showTitleBar;
            return this;
        }

        public Builder setTitleName(String titleName) {
            this.titleName = titleName;
            return this;
        }

        public Builder setBackDrawableId(int backDrawableId) {
            this.backDrawableId = backDrawableId;
            return this;
        }

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setCanRefresh(boolean canRefresh) {
            isCanRefresh = canRefresh;
            return this;
        }

        public Builder setTitleTextColor(int titleTextColor) {
            this.titleTextColor = titleTextColor;
            return this;
        }

        public Builder setTitleBackground(int titleBackground) {
            this.titleBackground = titleBackground;
            return this;
        }

        public Builder setRefreshColor(int refreshColor) {
            this.refreshColor = refreshColor;
            return this;
        }

        public Builder setNetworkDisconnectedTip(String networkDisconnectedTip) {
            this.networkDisconnectedTip = networkDisconnectedTip;
            return this;
        }

        public Builder setNetworkDisconnectedDrawable(int networkDisconnectedDrawable) {
            this.networkDisconnectedDrawable = networkDisconnectedDrawable;
            return this;
        }

        public Builder setJsWebViewName(String jsWebViewName) {
            this.jsWebViewName = jsWebViewName;
            return this;
        }

        public WebViewConfig build() {
            WebViewConfig webViewConfig = new WebViewConfig();
            webViewConfig.url = url;
            webViewConfig.backDrawableId = backDrawableId;
            webViewConfig.isCanRefresh = isCanRefresh;
            webViewConfig.isShowTitleBar = isShowTitleBar;
            webViewConfig.titleName = titleName;
            webViewConfig.titleBackground = titleBackground;
            webViewConfig.titleTextColor = titleTextColor;
            webViewConfig.refreshColor = refreshColor;
            webViewConfig.networkDisconnectedTip = networkDisconnectedTip;
            webViewConfig.networkDisconnectedDrawable = networkDisconnectedDrawable;
            webViewConfig.jsWebViewName = jsWebViewName;
            return webViewConfig;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(this.isShowTitleBar ? (byte) 1 : (byte) 0);
        dest.writeString(this.titleName);
        dest.writeInt(this.backDrawableId);
        dest.writeString(this.url);
        dest.writeByte(this.isCanRefresh ? (byte) 1 : (byte) 0);
        dest.writeInt(this.titleTextColor);
        dest.writeInt(this.titleBackground);
        dest.writeInt(this.refreshColor);
        dest.writeString(this.networkDisconnectedTip);
        dest.writeInt(this.networkDisconnectedDrawable);
        dest.writeString(this.jsWebViewName);
    }

    public void readFromParcel(Parcel source) {
        this.isShowTitleBar = source.readByte() != 0;
        this.titleName = source.readString();
        this.backDrawableId = source.readInt();
        this.url = source.readString();
        this.isCanRefresh = source.readByte() != 0;
        this.titleTextColor = source.readInt();
        this.titleBackground = source.readInt();
        this.refreshColor = source.readInt();
        this.networkDisconnectedTip = source.readString();
        this.networkDisconnectedDrawable = source.readInt();
        this.jsWebViewName = source.readString();
    }

    public WebViewConfig() {
    }

    protected WebViewConfig(Parcel in) {
        this.isShowTitleBar = in.readByte() != 0;
        this.titleName = in.readString();
        this.backDrawableId = in.readInt();
        this.url = in.readString();
        this.isCanRefresh = in.readByte() != 0;
        this.titleTextColor = in.readInt();
        this.titleBackground = in.readInt();
        this.refreshColor = in.readInt();
        this.networkDisconnectedTip = in.readString();
        this.networkDisconnectedDrawable = in.readInt();
        this.jsWebViewName = in.readString();
    }

    public static final Parcelable.Creator<WebViewConfig> CREATOR = new Parcelable.Creator<WebViewConfig>() {
        @Override
        public WebViewConfig createFromParcel(Parcel source) {
            return new WebViewConfig(source);
        }

        @Override
        public WebViewConfig[] newArray(int size) {
            return new WebViewConfig[size];
        }
    };
}
